package com.jacobson.huobi.mvp.adapters

import android.content.Context
import android.graphics.Color
import android.graphics.Paint
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.jacobson.core.di.annotation.ActivityScope
import com.jacobson.core.widget.adapter.ListRecyclerAdapter
import com.jacobson.huobi.R
import com.jacobson.huobi.components.CommonListRecyclerAdapter
import com.jacobson.huobi.entities.*
import com.jacobson.huobi.mvp.contract.VipContract
import com.jacobson.huobi.utils.ResourcesUtil
import kotlinx.android.synthetic.main.item_vip_recharge.view.*
import java.text.DecimalFormat
import javax.inject.Inject
import javax.inject.Named


@ActivityScope
class VipReChargeAdapter @Inject constructor(@Named("context") context: Context) :
    CommonListRecyclerAdapter<VipRechargeInfo>(context) {
    @field:Transient
    private var expandPosition: Int = 0

    override fun onCreateContentViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ContentViewHolder<VipRechargeInfo> =
        ViewHolder(context, parent)

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        (holder as? ViewHolder)?.setExpendPosition(this.expandPosition)
        super.onBindViewHolder(holder, position)
    }

    fun setExpandPosition(position: Int) {
        if (this.expandPosition == position) {
            this.expandPosition = 0
        } else {
            this.expandPosition = position
        }
        notifyDataSetChanged()
    }

    class ViewHolder(
        context: Context?,
        parent: ViewGroup
    ) : ListRecyclerAdapter.ContentViewHolder<VipRechargeInfo>(context, parent, R.layout.item_vip_recharge) {

        private var expendPosition: Int = 0

        fun setExpendPosition(position: Int) {
            this.expendPosition = position
        }

        override fun onBindView(items: List<VipRechargeInfo>, item: VipRechargeInfo, position: Int) {
            with(itemView) {
                tvDescription.text = item.description
                tvDurationDesc.text = item.durationDesc
                tvRealPrice.text = ("$" + ResourcesUtil.formatString(R.string.fm_symbol_double_one, item.realPrice))
                tvNominalPrice.text = ("$" + ResourcesUtil.formatString(R.string.fm_symbol_double_one, item.nominalPrice))
                tvNominalPrice.paint.flags = Paint.STRIKE_THRU_TEXT_FLAG //设置中划线
                if (position == expendPosition)  {
                    this.setBackgroundResource(R.drawable.ic_vip_recharge_tab_item_selected)
                }else {
                    this.setBackgroundColor(Color.WHITE)
                }
            }
        }


    }
}

private val decimalFormat = DecimalFormat("###################.###########")
private fun Double.ignoreEndZero() = decimalFormat.format(this)
